'use strict';
const vkPay = require("vk-uni-pay");
const YunGouPay = require('../../../../application/service/YunGouPay.js')
module.exports = {
	/**
	 * 此函数名称
	 * @url user/pub/test1 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------

		let oid = data.oid

		//订单
		let order = await vk.baseDao.findById({
			dbName: "uni-id-base-order",
			id: oid,
			whereJson: {
				"user_id": uid,
				"status": 1,
			}
		});

		//个人支付小程序使用
		let openid = null

		//微信网页 或小程序支付传递openid
		if (data.provider == 'wxpay') {
			if (data.openid) {
				openid = data.openid
			} else {
				if (userInfo.wx_openid && userInfo.wx_openid['h5'])
					openid = userInfo.wx_openid['h5']

				//微信小程序支付	
				if (userInfo.wx_openid && userInfo.wx_openid['mp-weixin'] && data.clientType == 'mp')
					openid = userInfo.wx_openid['mp-weixin']
			}
		}



		let PayContex = vkPay
		//官方支付
		if (data.provider == 'wxpay' || data.provider == 'alipay') {
			PayContex = vkPay
		}


		//个人支付
		if (data.provider == 'YunGouOS_wxpay' || data.provider == 'YunGouOS_alipay') {
			PayContex = YunGouPay
		}

		//创建支付信息
		res = await PayContex.createPayment({
			context: originalParam.context,
			provider: data.provider,
			data: {
				openid: openid,
				out_trade_no: oid,
				total_fee: order.total_fee, // 订单金额(单位分 100 = 1元)
				subject: order.title,
				type: order.type,
				custom: {
					user_id: uid,
					platform: data.runtime
				}
			}
		});

		//更新订单信息 支付渠道 - 更新时间 - transaction_id
		order = await vk.baseDao.updateById({
			dbName: "uni-id-base-order",
			id: oid,
			dataJson: {
				pay_type: data.provider,
				platform: data.runtime,
				update_time: new Date().getTime()
			},
			getUpdateData: true
		});

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}